package com.guiwu.gteway.industryapp.Map;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.mapapi.model.LatLng;
import com.guiwu.gteway.industryapp.R;

/**
 * Created by 乔冠 on 2017/12/20. 测试地图
 */

public class MapActivityTest extends FragmentActivity {
    private LocationClient mLocationClient;
    private boolean isFirstLocate = true;
    private FragmentManager fm;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_test_layout);

        initMapLocation();
    }



    /**
     * 设置map定位
     */
    private void initMapLocation(){
        mLocationClient = new LocationClient(getApplicationContext());
        mLocationClient.registerLocationListener(new MylocationListener());
        setLocationOption();
        requestLocation();
    }

    /**
     * 开始定位
     */
    private void requestLocation(){
        mLocationClient.start();
    }

    /**
     * 设置定位参数
     */
    private void setLocationOption(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setScanSpan(3000);
        option.setCoorType("bd09ll");
        option.setOpenGps(true);
        option.setIgnoreKillProcess(true);
        mLocationClient.setLocOption(option);
    }

    /**
     * 定义定位回调类
     */
    private class MylocationListener extends BDAbstractLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            StringBuilder currentPostion = new StringBuilder();
            currentPostion.append("纬度：").append(bdLocation.getLatitude()).append("\n");
            currentPostion.append("经线：").append(bdLocation.getLongitude()).append("\n");
            currentPostion.append("定位方式");
            if (bdLocation.getLocType() == BDLocation.TypeGpsLocation){
                currentPostion.append("GPS");
            }else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation){
                currentPostion.append("网络");
            }


            if (isFirstLocate){
                LatLng latLng = new LatLng(bdLocation.getAltitude(),bdLocation.getLatitude());

                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(latLng).zoom(16);

                BaiduMapOptions bo = new BaiduMapOptions().mapStatus(builder.build()).zoomControlsEnabled(true);
                fm = getSupportFragmentManager();
                // GuiwuMapFragment mapFragment = (GuiwuMapFragment) fm.findFragmentById(R.id.map_test);

                SupportMapFragment mapFragment = SupportMapFragment.newInstance(bo);
                fm.beginTransaction().add(R.id.map_test,mapFragment).commit();
                isFirstLocate = false;
            }


        }
    }
}
